importScripts('include/config.js');
importScripts('API/js/fonctions.js');
importScripts('js/fonctions.js');

function messageHandler(event) {
    try {
        // On récupère le message envoyé par la page principale
        var messageSent = event.data;

        // On teste la commande envoyée
        switch (messageSent.cmd) {
            case 'init':
                // On peut initialiser certaines parties de nos objets qui serviront
                // dans ce worker (attention au scope cependant !)
                break;
            case 'stop':
                self.close(); // Terminates the worker.
                break;
            case 'lancer':
                var entree = new Date;
                var entree = entree.getTime();
                
                var tabInput = { code_user : "" };
                var retour = callBD(domaine+"phpsql/mysql_getTicketsSupDebMail.php", "GET", "text", tabInput, "", false); 
                
                var labDate = getStrDateFR();

                //[x][0]responsable 
                //[x][1]nom 
                //[x][2]prenom 
                //[x][3]mail 
                for (var indice in retour){
                    var percent = Math.round((indice / (retour.length+1)) * 100);
                    this.postMessage(new WorkerMessage('avancement', percent));
                    
                    var tabInput = { code_user : retour[indice][0] };
                    var retour_TicketsSupDeb = callBD(domaine+"phpsql/mysql_getTicketsSupDeb.php", "GET", "text", tabInput, "", false);

                    var strhtml = "";

                    //[x][0]id_toc_ori 
                    //[x][1]id_toc 
                    //[x][2]revision 
                    //[x][3]responsable 
                    //[x][4]date_creation 
                    //[x][5]date_statut 
                    //[x][6]libelle
                    //[x][7]sn3 
                    //[x][8]code_user 
                    //[x][9]date_creation
                    //[x][10]date_modif

                    strhtml += "<style>";
                    strhtml += "table,th,td";
                    strhtml += "{";
                    strhtml += "border:1px solid black;";
                    strhtml += "}";
                    strhtml += "</style>";

                    strhtml += "<center>";
                    strhtml += "<table>";
                    strhtml += "<thead>";
                    strhtml += "<TR>";
                    strhtml += "<TH>N&deg;</TH>";
                    strhtml += "<TH>Id</TH>";
                    strhtml += "<TH>R&eacute;vision</TH>";
                    strhtml += "<TH>Libell&eacute;</TH>";
                    strhtml += "<TH>P&eacute;rim&egrave;tre SN3</TH>";
                    strhtml += "</thead>";
                    strhtml += "<tbody>";
                    var i = 0;
                    for (var indice_TSD in retour_TicketsSupDeb) {
                        strhtml += "<TR onMouseOver=\"changeCouleur(this);\" onMouseOut=\"remetCouleur(this);\" >";
                        i++;
                        strhtml += "<TD>"+i+"</TD>";
                        strhtml += "<TD><a target=\"_new\" href=\"http://10.197.68.30:4000/AutoDiseWeb/automate/popupModule.jsp?modType=SIG_SIG&modCode=" + retour_TicketsSupDeb[indice_TSD][0] + "\">" + retour_TicketsSupDeb[indice_TSD][1] + "</a> </TD>";
                        strhtml += "<TD>"+retour_TicketsSupDeb[indice_TSD][2]+"</TD>";
                        strhtml += "<TD>"+retour_TicketsSupDeb[indice_TSD][6].substring(0,60);+"</TD>";
                        strhtml += "<TD>";
                        strhtml += "<fieldset>";
                        var strChecked = "";
                        if(retour_TicketsSupDeb[indice_TSD][7] == "1"){
                            strChecked = "checked=\"checked\"";
                        }
                        strhtml += "<input type=\"radio\" name=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"\" id=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"_1\" value=\"1\" "+strChecked+" />";
                        strhtml += "<label for=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"_1\">Oui</label>";
                        var strChecked = "";
                        if(retour_TicketsSupDeb[indice_TSD][7] == "0"){
                            strChecked = "checked=\"checked\"";
                        }
                        strhtml += "<input type=\"radio\" name=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"\" id=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"_0\" value=\"0\" "+strChecked+" />";
                        strhtml += "<label for=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"_0\">Non</label>";
                        strhtml += "</fieldset>";
                        strhtml += "</TD>";
                        strhtml += "</TR>";
                    }
                    strhtml += "</tbody>";
                    strhtml += "</table>";
                    strhtml += "<a href='http://10.85.32.5/ODA_DISE/page_boad.html'>Rendez-vous sur #Menu / #Consultation / #Sa Board / #Vos tickets en Support/D&eacute;blocage</a>";
                    strhtml += "</center>";

                    var email_mail_ori = "";
                    var email_labelle_ori = "";
                    var email_mail_reply = "";
                    var email_labelle_reply = "";
                    var email_mails_dest = "";
                    var email_mails_copy = "";
                    var email_mails_cache = "";
                    var message_txt = "";
                    var message_html = "";
                    var sujet = "";

                    email_mail_ori = "oda.service.mail@cgi.com";
                    email_labelle_ori = "Service Mail ODA";
                    email_mail_reply = "oda.service.mail@cgi.com";
                    email_labelle_reply = "Service Mail ODA";

                    email_mails_dest += retour[indice][3]+",";

                    message_txt = "Anomalie avec le support du HTML.";

                    message_html = "<html><head></head><body>"+strhtml+"</body></html>";

                    sujet = "[ODA]Rappel des tickets en Support/Déblocage du "+labDate+".";

                    var retour_send_mail = send_mail(email_mail_ori,email_labelle_ori,email_mail_reply,email_labelle_reply,email_mails_dest,email_mails_copy,email_mails_cache,message_txt,message_html,sujet);
                    sleep(2000);
                }
                
                var percent = Math.round((indice / (retour.length+1)) * 100);
                this.postMessage(new WorkerMessage('avancement', percent));

                var tabInput = { code_user : "" };
                var retour_TicketsSupDeb = callBD(domaine+"phpsql/mysql_getTicketsSupDeb.php", "GET", "text", tabInput, "", false);

                var strhtml = "";

                //[x][0]id_toc_ori 
                //[x][1]id_toc 
                //[x][2]revision 
                //[x][3]responsable 
                //[x][4]date_creation 
                //[x][5]date_statut 
                //[x][6]libelle
                //[x][7]sn3 
                //[x][8]code_user 
                //[x][9]date_creation
                //[x][10]date_modif

                strhtml += "<style>";
                strhtml += "table,th,td";
                strhtml += "{";
                strhtml += "border:1px solid black;";
                strhtml += "}";
                strhtml += "</style>";

                strhtml += "<center>";
                strhtml += "<table>";
                strhtml += "<thead>";
                strhtml += "<TR>";
                strhtml += "<TH>N&deg;</TH>";
                strhtml += "<TH>Id</TH>";
                strhtml += "<TH>R&eacute;vision</TH>";
                strhtml += "<TH>Libell&eacute;</TH>";
                strhtml += "<TH>Responsable</TH>";
                strhtml += "<TH>P&eacute;rim&egrave;tre SN3</TH>";
                strhtml += "</thead>";
                strhtml += "<tbody>";
                var i = 0;
                for (var indice_TSD in retour_TicketsSupDeb) {
                    strhtml += "<TR onMouseOver=\"changeCouleur(this);\" onMouseOut=\"remetCouleur(this);\" >";
                    i++;
                    strhtml += "<TD>"+i+"</TD>";
                    strhtml += "<TD><a target=\"_new\" href=\"http://10.197.68.30:4000/AutoDiseWeb/automate/popupModule.jsp?modType=SIG_SIG&modCode=" + retour_TicketsSupDeb[indice_TSD][0] + "\">" + retour_TicketsSupDeb[indice_TSD][1] + "</a> </TD>";
                    strhtml += "<TD>"+retour_TicketsSupDeb[indice_TSD][2]+"</TD>";
                    strhtml += "<TD>"+retour_TicketsSupDeb[indice_TSD][6].substring(0,60);+"</TD>";
                    strhtml += "<TD>"+retour_TicketsSupDeb[indice_TSD][3]+"</TD>";
                    strhtml += "<TD>";
                    strhtml += "<fieldset>";
                    var strChecked = "";
                    if(retour_TicketsSupDeb[indice_TSD][7] == "1"){
                        strChecked = "checked=\"checked\"";
                    }
                    strhtml += "<input type=\"radio\" name=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"\" id=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"_1\" value=\"1\" "+strChecked+" />";
                    strhtml += "<label for=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"_1\">Oui</label>";
                    var strChecked = "";
                    if(retour_TicketsSupDeb[indice_TSD][7] == "0"){
                        strChecked = "checked=\"checked\"";
                    }
                    strhtml += "<input type=\"radio\" name=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"\" id=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice_TSD][2]+"_0\" value=\"0\" "+strChecked+" />";
                    strhtml += "<label for=\"InputRadio_"+retour_TicketsSupDeb[indice_TSD][1]+"_"+retour_TicketsSupDeb[indice][2]+"_0\">Non</label>";
                    strhtml += "</fieldset>";
                    strhtml += "</TD>";
                    strhtml += "</TR>";
                }
                strhtml += "</tbody>";
                strhtml += "</table>";
                strhtml += "<a href='http://10.85.32.5/ODA_DISE/page_gestion_supp_debl.html'>Rendez-vous sur #Menu / #Gestion / #Gestion Support/Déblocage</a>";
                strhtml += "</center>";

                var email_mail_ori = "";
                var email_labelle_ori = "";
                var email_mail_reply = "";
                var email_labelle_reply = "";
                var email_mails_dest = "";
                var email_mails_copy = "";
                var email_mails_cache = "";
                var message_txt = "";
                var message_html = "";
                var sujet = "";

                email_mail_ori = "oda.service.mail@cgi.com";
                email_labelle_ori = "Service Mail ODA";
                email_mail_reply = "oda.service.mail@cgi.com";
                email_labelle_reply = "Service Mail ODA";

                var returnSql = new Array();
                var sql = "SELECT mail FROM `tab_utilisateurs` a, tab_service_mail_dest b WHERE 1=1 AND a.code_user = b.code_user AND b.id_type_mail = 5 AND nivo = 1";
                returnSql = getSQL(sql);
                for (var indice in returnSql) {
                    email_mails_dest += returnSql[indice]+",";
                }

                var returnSql = new Array();
                var sql = "SELECT mail FROM `tab_utilisateurs` a, tab_service_mail_dest b WHERE 1=1 AND a.code_user = b.code_user AND b.id_type_mail = 5 AND nivo = 2";
                returnSql = getSQL(sql);
                for (var indice in returnSql) {
                    email_mails_copy += returnSql[indice]+",";
                }

                var returnSql = new Array();
                var sql = "SELECT mail FROM `tab_utilisateurs` a, tab_service_mail_dest b WHERE 1=1 AND a.code_user = b.code_user AND b.id_type_mail = 5 AND nivo = 3";
                returnSql = getSQL(sql);
                for (var indice in returnSql) {
                    email_mails_cache += returnSql[indice]+",";
                }

                message_txt = "Anomalie avec le support du HTML.";

                message_html = "<html><head></head><body>"+strhtml+"</body></html>";

                sujet = "[ODA]Rappel des tickets en Support/Déblocage du "+labDate+".";

                var retour_send_mail = send_mail(email_mail_ori,email_labelle_ori,email_mail_reply,email_labelle_reply,email_mails_dest,email_mails_copy,email_mails_cache,message_txt,message_html,sujet);
                sleep(2000);
                
                var fin = new Date;
                var fin = fin.getTime();
                var secondes = Math.round((fin-entree)/1000);
                var hhmmss = convertSecondsToTime(secondes);
                this.postMessage(new WorkerMessage('stats',hhmmss));

                this.postMessage(new WorkerMessage('resultats',"salut"));
                break;
            }
    }catch (er) {
        this.postMessage("Erreur : " + er.message);
        log(0, "ERROR(WorkerEnvoiRappelSD):" + er.message);
    }
}

// On définit la fonction à appeler lorsque la page principale nous sollicite
this.addEventListener('message', messageHandler, false);